<?php

namespace app\models;

use app\config\Constants;
use Yii;
use yii\base\NotSupportedException;
use yii\web\IdentityInterface;
use yii\web\UnauthorizedHttpException;

/**
 * This is the model class for table "user".
 *
 * @property int $user_id 自增ID
 * @property string $thumbnail 头像
 * @property string $username 用户名称
 * @property string $mobile 手机号
 * @property int $status 用户状态:1禁用,2正常
 * @property string $add_time 添加时间
 *
 * @property UserAddress[] $userAddresses
 * @property UserCredential[] $userCredentials
 * @property UserInfoStore $userInfoStore
 * @property UserInfoUser $userInfoUser
 * @property UserWechatInfo $userWechatInfo
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        if($token->getClaim("credential") != Constants::USER){
            throw new UnauthorizedHttpException(Yii::t("app","令牌凭证错误"));
        }
        return self::findIdentity($token->getClaim("uid"));
    }

    public function getId()
    {
        return $this->user_id;
    }

    public function getAuthKey()
    {
        throw new NotSupportedException("不支持该方法");
    }

    public function validateAuthKey($authKey)
    {
        throw new NotSupportedException("不支持该方法");
    }

    /**
     * Gets query for [[UserAddresses]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserAddresses()
    {
        return $this->hasMany(UserAddress::className(), ['user_id' => 'user_id']);
    }

    /**
     * Gets query for [[UserCredentials]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserCredentials()
    {
        return $this->hasMany(UserCredential::className(), ['user_id' => 'user_id']);
    }

    /**
     * Gets query for [[UserInfoStore]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserInfoStore()
    {
        return $this->hasOne(UserInfoStore::className(), ['user_id' => 'user_id']);
    }

    /**
     * Gets query for [[UserInfoUser]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserInfoUser()
    {
        return $this->hasOne(UserInfoUser::className(), ['user_id' => 'user_id']);
    }

    /**
     * Gets query for [[UserWechatInfo]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserWechatInfo()
    {
        return $this->hasOne(UserWechatInfo::className(), ['user_id' => 'user_id']);
    }
}
